#!/bin/sh
case "$1" in
  start)
    mkdir -p /tmp/service /tmp/run /service/.s6-svscan
    # TODO(apenwarr): maybe actually do something in these
    : >/service/.s6-svscan/finish
    : >/service/.s6-svscan/crash
    chmod 755 /service/.s6-svscan/finish /service/.s6-svscan/crash
    # We don't run this under a babysitter because it's just too many
    # layers of babysitters.  And s6-svscan tends to return nonzero when
    # you ask it to shut down, even politely, which compounds problems
    # instead of helping.
    s6-svscan /service 2>&1 | logos svscan &

    # Start starting services
    {
      s6-rc-init /service &&
      s6-rc -u change all
    } 2>&1 | logos svscan &  # exits once all services are started
    ;;
  stop)
    { [ ! -e /service/.s6-svscan/lock ] || s6-svscanctl -q /service; } &&
    rm -rf /tmp/service /run/s6-rc /run/s6-rc:*
    ;;
  restart|reload)
    ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
esac
